package net.bingosoft.common.security.tag.toolbar;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import net.bingosoft.common.config.Config;
import net.bingosoft.common.util.ClassUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * 2007-9-24 上午10:56:42
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:Toolbar 标签
 * ChangeLog:
 */

public class ToolBarTag extends BodyTagSupport {
	protected static final Log log = LogFactory.getLog(ToolBarTag.class);
	private static final long serialVersionUID = 1L;
	private String toolBarId;
	private String styleClass;
	private String style;
	private boolean visible = true;
	private List <ToolTag>children = new ArrayList<ToolTag>();
	private BarPainter painter = (BarPainter)ClassUtil.newInstance(Config.getString("BAR_PAINTER"));


	/**
	 * get request
	 * @return
	 */
	public HttpServletRequest getRequest(){
		return (HttpServletRequest)pageContext.getRequest();
	}

	/**
	 * 添加一个子节点
	 * @param child
	 * @return
	 */
	public void addChild(ToolTag child){
		children.add(child.clone());			
	}

	/**
	 * @return the toolBarId
	 */
	public String getToolBarId() {
		return toolBarId;
	}
	/**
	 * @param toolBarId the toolBarId to set
	 */
	public void setToolBarId(String toolBarId) {
		this.toolBarId = toolBarId;
	}
	/**
	 * @return the styleClass
	 */
	public String getStyleClass() {
		return styleClass;
	}
	/**
	 * @param styleClass the styleClass to set
	 */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * @return the style
	 */
	public String getStyle() {
		return style;
	}
	/**
	 * @param style the style to set
	 */
	public void setStyle(String style) {
		this.style = style;
	}


	/**
	 * @return the visible
	 */
	public boolean isVisible() {
		return visible;
	}
	/**
	 * @param visible the visible to set
	 */
	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
	 */
	@Override
	public int doStartTag() throws JspException {
		//print head
		try {
			painter.printHeader(pageContext.getOut(),this);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.BodyTagSupport#doAfterBody()
	 */
	@Override
	public int doAfterBody() throws JspException {
		//printbody
		try {
			painter.printBody(pageContext.getOut(), this);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doAfterBody();
	}
	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag()
	 */
	@Override
	public int doEndTag() {
		//print tail
		try {
			painter.printFooter(pageContext.getOut(),this);
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.children.clear();
		return EVAL_PAGE;
	}

	public List<ToolTag> getChildren() {
		return children;
	}

}
